package com.aramco.fion.model.presentation
{
	import com.aramco.fion.event.LoadQueryEvent;
	import com.aramco.fion.vo.Callbacks;
	import com.aramco.fion.vo.DashboardVo;
	import com.aramco.fion.vo.FionVo;
	import com.aramco.fion.vo.GroupViewVO;
	import com.aramco.fion.vo.SummaryViewVO;
	
	import flash.events.IEventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	
	public class AssingComponentsPresentationModel
	{
		public function AssingComponentsPresentationModel()
		{
		}
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		[PostConstruct]
		public function preConstruct():void
		{
			var handlers:Callbacks = new Callbacks(resultHttp, faultHttp);
			dispatcher.dispatchEvent(new LoadQueryEvent(LoadQueryEvent.LOAD_XML_APP_EVENT,handlers));
		}
		
		private function resultHttp(data:*):void
		{
			trace(data);
		}
		
		private function faultHttp(event:FaultEvent):void
		{
			Alert.show(event.toString());
		}
		
		private var _appMeta:FionVo;
		
		[Inject("queryModel.appMeta", bind="true")]
		public function get appMeta():FionVo
		{
			return _appMeta;
		}
		
		public function set appMeta(value:FionVo):void
		{
			_appMeta = value;
			if(value != null)
			dataprovider = getNavigationXMLByComponents(value);
		}
		
		public var dataprovider:XML;
		
		public function getNavigationXMLByComponents(value:FionVo):XML
		{
			var xm:XML = <root></root>;
			for(var i:int = 0 ; i < value.dashboards.length;i++)
			{
				var dashboardList:XMLList = XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).id+'"'+ ' title="' +(value.dashboards[i] as DashboardVo).title+ '"></node>');
				trace((value.dashboards[i] as DashboardVo).title);
				for(var x:int = 0; x < (value.dashboards[i] as DashboardVo).views.length; x++)
				{
					var viewList:XMLList = new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x]['id'] +'"'+ 
						' title="' + (value.dashboards[i] as DashboardVo).views[x]['title'] +'"'+ '></node>');
					
					dashboardList.appendChild(viewList);
					
					
					trace('-'+(value.dashboards[i] as DashboardVo).views[x]['title']);
					if((value.dashboards[i] as DashboardVo).views[x] is SummaryViewVO)
					{
						//var compList:XMLList = 
						for(var y:int = 0 ; y < (value.dashboards[i] as DashboardVo).views[x].components.length; y++)
						{
							trace('--'+(value.dashboards[i] as DashboardVo).views[x].components[y]['title']);
							viewList.appendChild((new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].components[y]['id'] +'"'+ 
								' title="' + (value.dashboards[i] as DashboardVo).views[x].components[y]['title'] +'"'+ '></node>')));
						}
					}
						
					else if((value.dashboards[i] as DashboardVo).views[x] is GroupViewVO)
					{
						//
						for(var m:int = 0 ; m < (value.dashboards[i] as DashboardVo).views[x].groups.length; m++)
						{
							var groupList:XMLList = XMLList((('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].groups[m]['id'] +'"'+ 
								' title="' + (value.dashboards[i] as DashboardVo).views[x].groups[m]['title'] +'"'+ '></node>')));
							for(var n:int = 0; n < (value.dashboards[i] as DashboardVo).views[x].groups[m].components.length; n++)
							{
								trace('--'+(value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['title']);
								groupList.appendChild((new XMLList('<node id=' +'"'+ (value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['id'] +'"'+ 
									' title="' + (value.dashboards[i] as DashboardVo).views[x].groups[m].components[n]['title'] +'"'+ '></node>')));
							}
						}
						viewList.appendChild(groupList);
					}
					
				}
				xm.appendChild(dashboardList);
			}
			return xm;
		}
	}
}